package com.ipharma.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Vector;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.config.Config;

/**
 * A Swing-based top level window class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUIRecycle extends GUIOperation {

   /**
    * The Layouts.
    */
   GridBagLayout layoutBase = new GridBagLayout();

   /**
    * The main panel.
    */
   GUIButtonPanel jPanelButtons;

   GUITransactionRecycle jPanelTransactions;

   GUIBarCode jPanelBarCode;

   GUIButtonImage jButtOk;

   /**
    * Attributes
    */

   /**
    * Constructs a new instance.
    */
   public GUIRecycle(GUIWindow pContainer) {

      super(pContainer);
      try {
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      this.setLayout(layoutBase);
      this.setBackground(Colors.RCY_BACK);

      // panel: Buttons
      /*
       * DispenserService service = Helper.getDispenserService(); List productos =
       * service.getRetornosDelDispenser(new Long(this.dispenserId));
       */
      jPanelButtons = new GUIButtonPanel(this, theDispenser.getRecycleBin());
      jPanelButtons.setRequestFocusEnabled(false);
      jPanelButtons.setAllActive(false);

      // button: Action
      jButtOk = new GUIButtonImage(Images.buttDoEmpty, Images.buttDoEmptyDn);
      jButtOk.addActionListener(new GUIRecycle_jButtOk_actionAdapter(this));

      // panel: Transactions
      jPanelTransactions = new GUITransactionRecycle(this);
      jPanelTransactions.setRequestFocusEnabled(false);

      // panel: BarCode
      jPanelBarCode = new GUIBarCode(this);
      jPanelBarCode.requestFocus();
      jPanelBarCode.setActive(false);

      this.add(jPanelButtons, new GridBagConstraints2(0, 0, 1, 3, 5.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH,
            Texts.panelMargin, 0, 0));
      this.add(jButtOk, new GridBagConstraints2(1, 0, 1, 1, 0.1, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.BOTH, Texts.buttMargin,
            0, 0));
      this.add(jPanelTransactions, new GridBagConstraints2(1, 1, 1, 1, 0.1,
            1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH,
            Texts.transMargin, 0, 0));
      this.add(jPanelBarCode, new GridBagConstraints2(1, 2, 1, 1, 0.1, 0.0,
            GridBagConstraints.SOUTH, GridBagConstraints.BOTH,
            Texts.barCodeMargin, 0, 0));
   }

   /**
    * Starts execution
    */
   public void start() {

      try {
         // sets status bar message
         ((GUIMenu) myContainer).setStatusMsg(Texts.RCY_STS);

         // refreshes buttons if changed
         if (((GUIMenu) myContainer).dbRecycleChanged) {
            this.jPanelButtons.refresh(theDispenser.getRecycleBin());
            ((GUIMenu) myContainer).dbRecycleChanged = false;
         }

         this.setVisible(true);
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Deletes the recyclebin content
    */
   public void emptyRecycleBin() {

      int li_alert = -1;
      String ls_quest;
      GUIConfirmer l_sureDialog;

      try {
         // stop timer, dialogs controls their own timer
         myContainer.stopTimer();
         // confirms deletion of recycle bin contents
         l_sureDialog = new GUIConfirmer(this, Texts.CFR_FORSURE_RCY, true);
         ls_quest = "<p></p><p align='center'>" + Texts.CFR_FORSURE_HEAD
               + Texts.CFR_FORSURE_RCY + Texts.CFR_FORSURE_END;
         l_sureDialog.setQuestion(ls_quest);

         Config config = (Config) theSess.get(McdPrs.CONFIG);
         l_sureDialog.start(new Long(config.getTimeout()).longValue());

         if (l_sureDialog.hasExpired()) // time expired
         {
            myContainer.timeout();
            return;
         }
         if (l_sureDialog.getSelection() == false) // canceled
         {
            // reset timer
            myContainer.startTimer();
            return;
         }

         // updates dispenser recyclebin

         Vector vProducts = theDispenser.emptyRecycleBin();
         this.jPanelButtons.refresh(theDispenser.getRecycleBin());
         ((GUIMenu) myContainer).dbRecycleChanged = true;
         // registers a transaction for each product in recyclebin
         this.jPanelTransactions.addProducts(vProducts);
         this.jPanelTransactions.endTransaction(li_alert);

         // reset timer
         myContainer.startTimer();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Returns the type of operation
    * 
    * @return type of operation
    */
   public int getType() {

      return McdPrs.recycleId;
   }

   void jButtOk_actionPerformed(ActionEvent e) {

      // shows empty recycle bin confirmation dialog
      this.emptyRecycleBin();
   }
}

class GUIRecycle_jButtOk_actionAdapter implements java.awt.event.ActionListener {

   GUIRecycle adaptee;

   GUIRecycle_jButtOk_actionAdapter(GUIRecycle adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButtOk_actionPerformed(e);
   }
}
